package com.tklab.smartcity.ui.home;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.tklab.smartcity.bean.NewsBean;
import com.tklab.smartcity.bean.NewsCatagroy;
import com.tklab.smartcity.bean.Rotation;
import com.tklab.smartcity.bean.ServiceBean;
import com.tklab.smartcity.bean.WertherBean;
import com.tklab.smartcity.util.RetrofitClient;

public class HomeViewModel extends ViewModel {

    private MutableLiveData<Rotation> mrotation;
    private MutableLiveData<WertherBean> mWerther;
    private MutableLiveData<ServiceBean> mService;
    private MutableLiveData<NewsCatagroy> mNewCategroy;
    private MutableLiveData<NewsBean> mNewsBean;
    private Gson gson;

    public HomeViewModel() {
        mrotation = new MutableLiveData<>();
        mWerther=new MutableLiveData<>();
        mService=new MutableLiveData<>();
        mNewCategroy=new MutableLiveData<>();
        mNewsBean=new MutableLiveData<>();
        gson=new Gson();
    }
    public LiveData<NewsCatagroy> getNewsCatagroy() {
        String url="/prod-api/press/category/list";
        new RetrofitClient().get(url, new RetrofitClient.BackJson() {
            @Override
            public void success(String json) {
                NewsCatagroy newCategroy=gson.fromJson(json,NewsCatagroy.class);
                mNewCategroy.setValue(newCategroy);
            }
        });
        return mNewCategroy;
    }
    public LiveData<NewsBean> getNews() {

        return mNewsBean;
    }
    public void loadNews(String type){

        String url="/prod-api/press/press/list?type="+type;
       new RetrofitClient().get(url,(json)->{
           NewsBean newsBean=gson.fromJson(json,NewsBean.class);
            mNewsBean.setValue(newsBean);
       });
    }
    public LiveData<Rotation> getRotation() {
        String url="/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=2";
        new RetrofitClient().get(url, new RetrofitClient.BackJson() {
            @Override
            public void success(String json) {
                Rotation rotation=gson.fromJson(json,Rotation.class);
                mrotation.setValue(rotation);
            }
        });
        return mrotation;
    }
    public LiveData<WertherBean> getWerther() {
        String url="/prod-api/api/common/weather/today";
        new RetrofitClient().get(url, new RetrofitClient.BackJson() {
            @Override
            public void success(String json) {
                WertherBean wertherBean=gson.fromJson(json,WertherBean.class);
                mWerther.setValue(wertherBean);
            }
        });
        return mWerther;
    }
    public LiveData<ServiceBean> getService() {
        String url="/prod-api/api/service/list";
        new RetrofitClient().get(url, new RetrofitClient.BackJson() {
            @Override
            public void success(String json) {
                ServiceBean service=gson.fromJson(json,ServiceBean.class);
                mService.setValue(service);
            }
        });
        return mService;
    }
}